//
//  AppDelegate.swift
//  SinaWeibo23
//
//  Created by apple on 16/1/30.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        print(AccountViewModel.sharedAccountViewModel.userLogin)
        setThemeColor()
        //注册通知
        registerNotification()
        //实例化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        //可视化
        window?.makeKeyAndVisible()
        
        //设置根视图控制器
        window?.rootViewController = defaultRootViewController()
        return true
    }
    //消息机制才能够找到该方法
    @objc private func AppRootViewController(n: NSNotification) {
        window?.rootViewController = n.object == nil ? WelcomeViewController() : MainViewController()
    }
    //注册通知
    private func registerNotification() {
        // object 观测哪一个对象发出的通知  如果为 nil 默认是所有对象
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "AppRootViewController:", name: KChoseRootVC, object: nil)
    }
    private func defaultRootViewController() -> UIViewController {
        return AccountViewModel.sharedAccountViewModel.userLogin ? WelcomeViewController() : MainViewController()
    }
    
    private func setThemeColor() {
        //必须提前设置  在控件被实例化之前就需要设置渲染颜色
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
    //移除通知  编码规范而已
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
}

